/** @author siopo
 
	ScrollBar Class 

	version : 2.0
	edit = 2011/05/19

	
 */

package com.d4p.custom.customScrollBar
{
	
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	public class Scrollbar extends MovieClip
	{
		public var setTargetY:Number;
		public var totalList_num:Number;
		public var init_chk:Boolean = false;
		public var nowPageNum:int = 0;
		
		private var scroll_mode:Number = 0; // 0:기본, 1:구간
		private var startMousePoint:Number;
		private var startBarPoint:Number;
		private var _root:Object;
		private var target_mc:MovieClip;
		private var setTargetX:Number;
		private var targetMaskHeight:Number;
		private var exPos_num:Number;
		private var ag_num:Number;
		private var targetHeight_num:Number;
		private var move_fnc:Function;
		private var wheelSize_num:Number = 4;
		private var returnFunction:*;
		private var move_setNum:Number = 0;
		private var chkScrollNum:Number = -1;
		
		
		
		private var _scroll_mc:MovieClip;
		private var _bar_mc:MovieClip;
		private var _bg_mc:MovieClip;
		private var _down_mc:MovieClip;
		private var _up_mc:MovieClip;
		private var _moveSize_num:Number;
		
		//private var
		
		private var _js_wheel_chk:Boolean = false;
		
		public function Scrollbar():void
		{
		}
		
		public function init(		$scroll_mc:MovieClip,
									$root:*,
									$target:MovieClip, 
									$targetHeight_num:Number, 
									$maskHeight_num:Number, 
									moveFnc:Function = null, 
									thisType:Number = 0, 
									returnFn:Function = null,
									$moveSize_num:Number = 0, // 구간 움직일 칸 사이즈
									$wheelSize_num:Number = 2,
									$wheel_chk:Boolean = false
									):void {
			
			init_chk = true;
			
			wheelSize_num = $wheelSize_num;
			_scroll_mc = $scroll_mc;
			_bar_mc = $scroll_mc.bar_mc;
			_bg_mc = $scroll_mc.bg_mc;
			_down_mc = $scroll_mc.down_mc;
			_up_mc = $scroll_mc.up_mc;
			_moveSize_num = $moveSize_num;
			
			
			_root = $root;
			target_mc = $target;
			targetHeight_num = $targetHeight_num;
			targetMaskHeight = $maskHeight_num;
			move_fnc = moveFnc;
			
			setTargetX = target_mc.x;
			setTargetY = target_mc.y;
			
			_bar_mc.y = 0;
			_bar_mc.alpha = 1;
			
			_bar_mc.buttonMode = true;
			_bar_mc.addEventListener(MouseEvent.ROLL_OVER, barOver);
			_bar_mc.addEventListener(MouseEvent.ROLL_OUT, barOver);
			_bar_mc.addEventListener(MouseEvent.MOUSE_UP, barOver);
			_bar_mc.addEventListener(MouseEvent.MOUSE_DOWN, scrollStart);
			_bar_mc.addEventListener(MouseEvent.CLICK, scrollEnd);
			
			_bg_mc.buttonMode = true;
			_bg_mc.addEventListener(MouseEvent.MOUSE_DOWN, bgClick);
			
			// size
			var $setButtonSize_num:Number = _down_mc.height + _up_mc.height - 1;
			_bg_mc.height = targetMaskHeight - $setButtonSize_num;
			_down_mc.y = _bg_mc.height + 13;
			
			var height_num:Number = targetMaskHeight / 2 - (targetHeight_num / targetMaskHeight) * 16;
			if (height_num > (targetMaskHeight - $setButtonSize_num - ((targetMaskHeight - $setButtonSize_num) / 3))) {
				height_num = (targetMaskHeight - $setButtonSize_num - ((targetMaskHeight - $setButtonSize_num) / 3));
			}
			if (height_num < 40) {
				height_num = 40;
			}
			
			_bar_mc.height = int(height_num);
			//_bar_mc.c_mc.height = int(height_num) - 2;
			//_bar_mc.bot_mc.y = int(height_num) - 2;
			
			if (targetMaskHeight >= Math.floor(targetHeight_num)) {
				_scroll_mc.visible = false;
			} else {
				_scroll_mc.visible = true;
			}
			
			scroll_mode = thisType;
			
			if (returnFn != null) {
				returnFunction = returnFn;
			}
			
			_up_mc.addEventListener(MouseEvent.CLICK, upClick);
			_down_mc.addEventListener(MouseEvent.CLICK, downClick);
			
			//
			if (ExternalInterface.available && $wheel_chk) {
				_js_wheel_chk = true;
				ExternalInterface.addCallback("externalMouseEvent", externalMouseEvent);
			}
		}
		
		private function barOver(e:MouseEvent):void 
		{
			switch (e.type) 
			{
				case "rollOver":
					if(_scroll_chk) {
						//_bar_mc.c_mc.gotoAndStop(3);
						//_bar_mc.top_mc.gotoAndStop(3);
						//_bar_mc.bot_mc.gotoAndStop(3);
					}else {
						//_bar_mc.c_mc.gotoAndStop(2);
						//_bar_mc.top_mc.gotoAndStop(2);
						//_bar_mc.bot_mc.gotoAndStop(2);
						
					}
					break;
				case "rollOut":
				case "mouseUp":
					//_bar_mc.c_mc.gotoAndStop(1);
					//_bar_mc.top_mc.gotoAndStop(1);
					//_bar_mc.bot_mc.gotoAndStop(1);
					break;
			}
			
		}
		
		public function scrollReset(target, targetHeight2, targetHeight, returnFn:Function=null, thisType:Number=0):void {
			
			if (!init_chk) return;
			
			var tmp_height = targetHeight_num;
			var tmpHeight2_num:Number = _bar_mc.y / (_bg_mc.height - _bar_mc.height);
			
			target_mc = target;
			targetHeight_num = targetHeight2;
			targetMaskHeight = targetHeight;
			
			
			var $setButtonSize_num:Number = _down_mc.height + _up_mc.height - 1;
			var height_num:Number = targetMaskHeight / 2 - (targetHeight2 / targetMaskHeight) * 16;
			if (height_num > (targetMaskHeight - $setButtonSize_num - ((targetMaskHeight - $setButtonSize_num)/3))) {
				height_num = (targetMaskHeight - $setButtonSize_num - ((targetMaskHeight - $setButtonSize_num) / 3));
			}
			if (height_num < 40) {
				height_num = 40;
			}
			
			_bar_mc.height = int(height_num);
			//_bar_mc.c_mc.height = int(height_num) - 2;
			//_bar_mc.bot_mc.y = int(height_num) - 2;
			
			_bg_mc.height = targetMaskHeight - $setButtonSize_num;
			_down_mc.y = _bg_mc.height;
			
			
			_bar_mc.y = Math.round(_bar_mc.y / (targetHeight_num / tmp_height));
			_bar_mc.y = Math.round(tmpHeight2_num * (_bg_mc.height - _bar_mc.height));
			
			if (targetMaskHeight >= Math.floor(targetHeight_num)) {
				_scroll_mc.visible = false;
				_bar_mc.y = 0;
			} else {
				_scroll_mc.visible = true;
			}
			
			if (targetMaskHeight < _bar_mc.y + height_num) {
				
			}
			
			if (returnFn != null) {
				returnFunction = returnFn;
			}
			
			scroll_mode = thisType;
			
			
			//if (_bar_mc.height + _bar_mc.y > _bg_mc.height - _bar_mc.height) {
				//scrollMoving(_bg_mc.height - _bar_mc.height);
			//}
			startBarPoint = 0;
			moveBar(_bar_mc.y);
			
		}
		
		private function downClick(e:MouseEvent):void 
		{
			scrollMove(-1);
		}
		
		private function upClick(e:MouseEvent):void 
		{
			scrollMove(1);			
		}
		
		public function scrollTargetY(num):void
		{
			setTargetY = num;
		}
		
		private function wheelMove():void
		{
			if (move_fnc !== null) {
				move_fnc();
			}
		}
		
		public function wheelStart($target_mc = null):void 
		{
			if ($target_mc == null)$target_mc = target_mc;
			
			if (_js_wheel_chk) {
				//Debug.log("WHEEL JS STaRT");
				ExternalInterface.call("flashWheelOver");
			}
			
			else {
				stage.addEventListener(MouseEvent.MOUSE_WHEEL, scrollWheel2);
			}
			
		}
		
		public function externalMouseEvent(e):void
		{
			//Debug.log(e);
			
			if (Number(e) > 0) {
				scrollMove(1, false);
			} else {
				scrollMove(-1, false);
			}
		}
		
		private function scrollWheel2(e:MouseEvent):void 
		{
			if (e.delta > 0) {
				scrollMove(1, false);
			} else {
				scrollMove(-1, false);
			}
		}
		
		
		public function wheelEnd($target_mc = null):void 
		{
			if ($target_mc == null)$target_mc = target_mc;
			
			if (_js_wheel_chk) {
				//Debug.log("WHEEL JS EnD");
				ExternalInterface.call("flashWheelOut");
			} 
			else {
				stage.removeEventListener(MouseEvent.MOUSE_WHEEL, scrollWheel2)
				
			}
		}
		
		private function bgClick(e:MouseEvent):void 
		{
			scrollValue();
			
			var posY = int(_scroll_mc.mouseY - _bar_mc.height / 2);
			var setY:Number = 0;
			var wheelSize:Number = (_bg_mc.height - _bar_mc.height) / ((targetHeight_num - targetMaskHeight) / targetMaskHeight) / wheelSize_num;
			
			
			if (posY < 0) {
				posY = 0;
			} else if (posY > exPos_num) {
				posY = exPos_num;
			}
			nowPageNum = posY / wheelSize;
			
			// 움직이자..
			trace("SCROLL_MOUSE_MOVE : " + posY);
			scrollMoving(posY);
			scrollStart(null);
			
		}
		
		public function resetScroll():void {
			_bar_mc.y = 0;
			target_mc.y = setTargetY;
		}
		
		private function scrollWheel(e:MouseEvent):void 
		{
			var setNum:Number;
			if (e.delta > 0) {
				setNum=1;
			} else {
				setNum=-1;
			}
			
			scrollMove(setNum);
			
		}
		
		
		/*
		 * Scroll System..
		 * ---------------------------------------------------------------------------------------------------------------------  */
		
		public var thisPosition_num:Number = 0;
		public var totalPosition_num:Number = 0;
		public var setPoint_num:Number = 0;
		public var prevPoint_num:Number = -1;
		public var listPage_num:Number = -1;
		private var barYTmp_num:Number = -1;
		
		public function pagingMove():void
		{
			
		}
		private function scrollMoving(set_num: Number, barSpeed=.2, targetSpeed=.2, easing = "Cubic.easeOut", delay:Number = 0, updateFnc=null, cmpFnc=null, goSmoothing:Boolean=false):void
		{
			set_num = int(set_num);
			
			
			if (scroll_mode == 0 && !goSmoothing) {
				
				_bar_mc.y = set_num;
				setPoint_num = Math.ceil((setTargetY - Math.ceil( -set_num * ag_num + setTargetY)) / _moveSize_num);
				target_mc.y = -setPoint_num * _moveSize_num + setTargetY;
				
				moveUpdate();
				
				if (1 < ((_scroll_mc.mouseY - startMousePoint) + startBarPoint) / exPos_num) {
					pagingMove();
				}
				
			} else if (scroll_mode == 1 || goSmoothing) {
			//} else if (scroll_mode == 1) {
			
			
				
				_bar_mc.y = set_num;
				TweenMax.killTweensOf(_bar_mc);
				TweenMax.to(_bar_mc, barSpeed, { roundProps:["y"], y:set_num, overwrite:true, ease:easing, delay:delay, onUpdate:scrollMovingTarget, onUpdateParams:[targetSpeed, easing, delay, updateFnc, cmpFnc, move_setNum] } );
				
			}
			
			move_setNum = 0;
		}
		
		private function scrollMovingTarget(targetSpeed = .2, easing = "Cubic.easeOut", delay:Number = 0, updateFnc = null, cmpFnc = null, movSetNum:Number = 0 ):void
		{
				//trace("뭐냐고");
			//if (thisPosition_num !== Math.round((_bar_mc.y / exPos_num) * (targetHeight_num - targetMaskHeight))) {
				//trace("뭐냐고");
			
				thisPosition_num = Math.round((_bar_mc.y / exPos_num) * (targetHeight_num - targetMaskHeight ));
				setPoint_num = Math.ceil((setTargetY - Math.ceil( -_bar_mc.y * ag_num + setTargetY)));
				
				TweenMax.killTweensOf(target_mc);
				TweenMax.to(target_mc, targetSpeed, { roundProps:["y"], y: -setPoint_num + setTargetY, ease:easing, delay:delay, onUpdate:updateFnc, onComplete:cmpFnc } );
				
				moveUpdate();
			//}
		}
		 
		
		public function scrollTarget(num):void
		{
			move(num, 0, Cubic.easeOut);
		}
		
		public function scrollPlusMoving(num):void
		{
			
			var n = target_mc.y;
			
			if (num > 0) {
				target_mc.y -= _root.lineHeight_num;
			} else {
				target_mc.y += _root.lineHeight_num;
			}
		}
		
		private function moveBar(posNumber: Number ):void
		{
			scrollValue();
			var num:Number = (posNumber) + startBarPoint;
			
			if (num < 0) {
				num = 0;
			} else if (num > exPos_num) {
				num = exPos_num;				
			}
			
			
			// 움직이자..
			scrollMoving(num, 0, 0);
			
			wheelMove();
		}
		 
		
		public function scrollMove(num, $chk:Boolean = false ):void {
			
			//trace(num);
			
			if (!_scroll_mc.visible) return;
			
				scrollValue();
				
				var setY:Number = 0;
				var wheelSize:Number = (_bg_mc.height - _bar_mc.height) / ((targetHeight_num - targetMaskHeight) / targetMaskHeight) / wheelSize_num;
				
				if (num > 0) {
					if(nowPageNum>0) {
						nowPageNum--;
						setY = (wheelSize * nowPageNum);
					}
				} else {
					nowPageNum++;
					setY = (wheelSize * nowPageNum);
				}
				
				if (setY < 0) {
					setY = 0;
					nowPageNum = 0;
				} else if (setY > exPos_num) {
					nowPageNum--;
					setY = exPos_num;
					
					pagingMove();
					
				}
				
				if($chk) {
					scrollMoving(setY, .2, .2, "Cubic.easeOut", 0, null, null, true);
				} else {
					scrollMoving(setY);
				}
				wheelMove();
				
		}
		
		
		public function move(num: Number, delay_num:Number = 0, ease = null, time:Number = .2, time2:Number = .2, smooth:Boolean=false):void {
			
			if (ease == null)
				ease = Expo.easeInOut;
			
			scrollValue();
			
			move_setNum = num;
			
			//var setY:Number =  (num - setTargetY) / ag_num;
			var setY:Number =  (num) * ((_bg_mc.height - _bar_mc.height) / (targetHeight_num - targetMaskHeight));
			
			if (setY < 0) {
				setY = 0;
			} else if (setY > exPos_num) {
				setY = exPos_num;
			}
			
			scrollMoving(setY, time, time2, ease, delay_num, moveUpdate, moveUpdate, smooth);
			//wheelMove();
		}
		
		private function moveUpdate():void
		{
			
			var setY:Number = Math.round(_bg_mc.height * ((_bar_mc.y) / _bg_mc.height));
			var wheelSize2:Number = (_bg_mc.height - _bar_mc.height) / ((targetHeight_num - targetMaskHeight) / targetMaskHeight) / wheelSize_num;
			
			nowPageNum = setY / wheelSize2;
			//trace(target_mc.y);
			
			
			//trace("asdf");
			if (move_fnc !== null) {
				move_fnc();
			}
			
		}
		public function resetMove():void {
			
			scrollValue();
			
			nowPageNum = 0;
			
			scrollMoving(0, Cubic.easeOut, 0, _root.pageSetting);
			wheelMove();
		}
		
		private var _scroll_chk:Boolean = false;
		
		private function scrollStart(e:MouseEvent):void 
		{
			_scroll_chk = true;
			//_bar_mc.c_mc.gotoAndStop(3);
			//_bar_mc.top_mc.gotoAndStop(3);
			//_bar_mc.bot_mc.gotoAndStop(3);
			
			//trace(stage);
			startMousePoint = _scroll_mc.mouseY;
			startBarPoint = _bar_mc.y;
			
			_bar_mc.addEventListener(Event.ENTER_FRAME, scrollEvent);
			stage.addEventListener(MouseEvent.MOUSE_UP, scrollEnd);
			stage.addEventListener(Event.MOUSE_LEAVE, scrollEnd);
		}
		
		private function scrollEnd(e:MouseEvent):void 
		{
			_scroll_chk = false;
			_bar_mc.removeEventListener(Event.ENTER_FRAME, scrollEvent);
			stage.removeEventListener(MouseEvent.MOUSE_UP, scrollEnd);
			stage.removeEventListener(Event.MOUSE_LEAVE, scrollEnd);
			
		}
		
		private function scrollEvent(e:Event):void 
		{
			scrollValue();
			var num:Number = (_scroll_mc.mouseY - startMousePoint) + startBarPoint;
			
			if (num < 0) {
				num = 0;
			} else if (num > exPos_num) {
				num = exPos_num;				
			}
			
			
			scrollMoving(num, 0);
			wheelMove();
		}
		
		private function pageMoveDepth(num:Number):void {
			
			TweenMax.killTweensOf(target_mc);
			TweenMax.to(target_mc, .2, { roundProps:["y"], y:-75 * (num + 1) + setTargetY, overwrite:true, ease:Cubic.easeOut } );
			
			wheelMove();
			
		}
		
		
		private function scrollValue():void {
			
			exPos_num = _bg_mc.height - _bar_mc.height;
			ag_num = ((targetHeight_num)-targetMaskHeight) / exPos_num;
		}
		
	}
	
}